﻿using System;
using System.Runtime.CompilerServices;

namespace FixedMath
{
    public static class MathFloat
    {
        public static float PI = 3.14159265f;

        public static float Abs(float v)
        {
            return Math.Abs(v);
        }

        public static float Acos(float d)
        {
            return (float)Math.Acos(d);
        }

        public static float Asin(float d)
        {
            return (float)Math.Acos(d);
        }

        public static float Atan2(float v1, float v2)
        {
            return (float)Math.Atan2(v1, v2);
        }

        public static float Clamp(float value, float min, float max)
        {
            if (min > max)
                throw new ArgumentException("Clamp");
            if (value < min)
                return min;
            if (value > max)
                return max;
            return value;
        }

        public static int Clamp(int value, int min, int max)
        {
            if (min > max)
                throw new ArgumentException("Clamp");
            if (value < min)
                return min;
            if (value > max)
                return max;
            return value;
        }

        public static float Cos(float d)
        {
            return (float)Math.Cos(d);
        }

        public static float Max(float v1, float v2)
        {
            return Math.Max(v1, v2);
        }

        public static int Max(int v1, int v2)
        {
            return Math.Max(v1, v2);
        }

        public static float Min(float v1, float v2)
        {
            return Math.Min(v1, v2);
        }

        public static int Min(int v1, int v2)
        {
            return Math.Min(v1, v2);
        }

        public static float Sign(float value)
        {
            return Math.Sign(value);
        }

        public static float Sin(float a)
        {
            return (float)Math.Sin(a);
        }

        public static (float sp, float cp) SinCos(float x)
        {
            var sin = Sin(x);
            var cos = Cos(x);
            return (sin, cos);
        }

        public static float Sqrt(float d)
        {
            return (float)Math.Sqrt(d);
        }

        public static bool IsNormal(this float self)
        {
            // 将 float 转换为 int，并清除符号位
            int bits = Unsafe.As<float, int>(ref self) & 0x7FFFFFFF;

            // 检查是否为正常数
            return bits < 0x7F800000 && bits != 0 && (bits & 0x7F800000) != 0;
        }
    }
}